/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginUI.java
 *
 * Created on Apr 9, 2011, 1:49:46 PM
 */
package fss.client.ui;

import fss.util.rmi.RmiId;

/**
 *
 * @author hieu
 */
public class LoginUI extends javax.swing.JDialog
{
  private RmiId currentClient;

  /** Creates new form LoginUI */
  public LoginUI()
  {
    initComponents();
    setLocationRelativeTo(null);
  }

  public RmiId getRmiId()
  {
    return currentClient;
  }

  private void displayErrorMessage(String aMessage)
  {
    GenericDialog error = new GenericDialog(aMessage);
    error.setVisible(true);
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    hostLabel = new javax.swing.JLabel();
    hostTextField = new javax.swing.JTextField();
    portLabel = new javax.swing.JLabel();
    portTextField = new javax.swing.JTextField();
    nameLabel = new javax.swing.JLabel();
    nameTextField = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Sign In");
    setModal(true);

    hostLabel.setText("Host:");

    portLabel.setText("Port:");

    nameLabel.setText("Name:");

    jButton1.setText("Sign in");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(hostLabel, javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(nameLabel, javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(portLabel, javax.swing.GroupLayout.Alignment.TRAILING))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(portTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
              .addComponent(hostTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
              .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(hostLabel)
          .addComponent(hostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(portLabel))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(nameLabel))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jButton1)
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton1ActionPerformed
    {//GEN-HEADEREND:event_jButton1ActionPerformed
      if (!hostTextField.getText().isEmpty() &&
          !portTextField.getText().isEmpty() &&
          !nameTextField.getText().isEmpty())
      {
        boolean tPortValid = false;
        int tPortNum = 0;
        try
        {
          tPortNum = Integer.parseInt(portTextField.getText());
          if (tPortNum > 0 && tPortNum < 65535)
          {
            tPortValid = true;
          }
          else
          {
            displayErrorMessage("Port numer must be between 0 and 65535");
          }
        }
        catch (Exception aEx)
        {
          displayErrorMessage("Port numer needs to be numeric");
        }

        if (tPortValid)
        {
          currentClient = new RmiId(hostTextField.getText(),
                                  nameTextField.getText(),
                                  tPortNum);
          setVisible(false);
        }
      }
      else
      {
        displayErrorMessage(
          "At least one of the field is emtpy. Please fill in all field");
      }
    }//GEN-LAST:event_jButton1ActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel hostLabel;
  private javax.swing.JTextField hostTextField;
  private javax.swing.JButton jButton1;
  private javax.swing.JLabel nameLabel;
  private javax.swing.JTextField nameTextField;
  private javax.swing.JLabel portLabel;
  private javax.swing.JTextField portTextField;
  // End of variables declaration//GEN-END:variables
}
